package com.jy.wms.controller;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.ExportUtils;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.*;
import com.jy.wms.service.WmsSupportNumberReportExportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ExportController")
@Api(value = "/ExportController", description = "统计省份报表")
public class WmsSupportNumberReportExportController extends BaseController<WmsSupportNumberReportExportService, WmsProvinceRankingReport> {




    @PostMapping(value = "SelectBathPrimaryExcel")
    @ApiOperation(value = "直销省市排名报表导出", response = JSONObject.class, notes = "直销省市排名报表导出")
    public @ResponseBody
    JsonResult selectbathprimaryexcel(@RequestBody WmsProvinceRankingReport WmsProvinceRankingReport, HttpServletResponse response, HttpServletRequest request, HttpSession session) throws Exception {

        //获取当前操作用户的货主id
        List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = hz.get(0).getHzid();
        //获取仓库Id
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = userinfo.getUser().getOrgId().get(0).toString();
        try {
            String filename = "直销省市排名报表";
            List<WmsProvinceRankingReport> list = null;
            try {
                //获得SimpleDateFormat类，我们转换为yyyy-MM-dd的时间格式
                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
                //使用SimpleDateFormat的parse()方法生成Date
                list = baseService.baseselectbathprimarykeys(WmsProvinceRankingReport, hzid, ckid);
            } catch (Exception e) {
                e.printStackTrace();
            }
            ExportUtils reportExcel = new ExportUtils();
            reportExcel.excelExport(list, filename, WmsProvinceRankingReport1.class, 1, response, request);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return renderSuccess("导出成功");
    }



/*
    @ApiOperation(value = "各销售渠道订单状态汇总报表导出", response = JSONObject.class, notes = "订单状态汇总报表导出")
    @PostMapping(value = "/SalesChannelExcel")
    public @ResponseBody
    JsonResult saleschannelorderstatussummaryExcel(@RequestBody SalesChannelOrderStatusSummary SalesChannelOrderStatusSummary, HttpServletResponse response, HttpServletRequest request, HttpSession session) throws ServiceException {

        String filename = "各销售渠道订单状态汇总报表";
        //获取当前操作用户的货主id
        List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = hz.get(0).getHzid();
        //获取仓库Id
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = userinfo.getUser().getOrgId().get(0).toString();
        SalesChannelOrderStatusSummary.setCkid(ckid);
        SalesChannelOrderStatusSummary.setHzid(hzid);
        try {
            List<SalesChannelOrderStatusSummary> list = baseService.saleschannelorderstatussummary(SalesChannelOrderStatusSummary);

            ExportUtils reportExcel = new ExportUtils();
            reportExcel.excelExport(list, filename, SalesChannelOrderStatusSummary.class, 1, response, request);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return renderSuccess("导出成功");
    }
*/

    @PostMapping(value = "StatisticsInVarious")
    @ApiOperation(value = "各省运费统计（按产品大类）导出报表", response = JSONObject.class, notes = "各省运费统计导出报表")
    public @ResponseBody
    void StatisticsInVariousProvinces(@RequestBody WmsSummaryOfTheSituationInEachProvince WmsSummaryOfTheSituationInEachProvince, HttpSession session, HttpServletResponse response) throws IOException {
        try {
            //获取当前操作用户的货主id
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid = hz.get(0).getHzid();
            //获取仓库Id
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = userinfo.getUser().getOrgId().get(0).toString();

            WmsSummaryOfTheSituationInEachProvince.setCkid(ckid);
            WmsSummaryOfTheSituationInEachProvince.setHzid(hzid);
            // 告诉浏览器用什么软件可以打开此文件
            response.setHeader("content-Type", "application/vnd.ms-excel");
            // 下载文件的默认名称
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("各省运费统计报表", "UTF-8") + ".xls");
            //编码
            response.setCharacterEncoding("UTF-8");
            ExportParams ep = new ExportParams();
            ep.setStyle(ManySheetOneSyler.class);

            List<ExcelExportEntity> Head = baseService.listHeader2(WmsSummaryOfTheSituationInEachProvince);
            List<Map<String, Object>> FreightData = baseService.StatisticsInVariousProvinces(WmsSummaryOfTheSituationInEachProvince);
            Workbook workbook = ExcelExportUtil.exportExcel(ep, Head, FreightData);
            workbook.write(response.getOutputStream());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping(value = "PmentSineachProvince")
    @ApiOperation(value = "各省支数统计（按产品大类）导出报表", response = JSONObject.class, notes = "各省支数统计导出报表")
    public @ResponseBody
    void pmentsineachprovince(@RequestBody Map<String, Object> mapp, HttpSession session, HttpServletResponse response) throws IOException {
        try {
        //获取当前操作用户的货主id
        List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = hz.get(0).getHzid();
        //获取仓库Id
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = userinfo.getUser().getOrgId().get(0).toString();
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("各省支数统计报表", "UTF-8") + ".xls");
        //编码
        response.setCharacterEncoding("UTF-8");
        ExportParams ep = new ExportParams();
        ep.setStyle(ManySheetOneSyler.class);

        List<ExcelExportEntity> Head = baseService.zishuHeader(mapp, hzid, ckid);
        List<Map<String, Object>> PSCP=baseService.pmentsineachprovince(mapp, hzid, ckid);
        Workbook workbook = ExcelExportUtil.exportExcel(ep, Head, PSCP);
        workbook.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping(value = "StatisticsOfTheBranches")
    @ApiOperation(value = "各省销售额统计（按产品大类）导出报表", response = JSONObject.class, notes = "各省销售额统计导出报表")
    public @ResponseBody
    void StatisticsOfTheBranches(@RequestBody Map<String, Object> mapp, HttpSession session, HttpServletResponse response) throws IOException {
        try {
            //获取当前操作用户的货主id
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid = hz.get(0).getHzid();
            //获取仓库Id
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = userinfo.getUser().getOrgId().get(0).toString();
            // 告诉浏览器用什么软件可以打开此文件
            response.setHeader("content-Type", "application/vnd.ms-excel");
            // 下载文件的默认名称
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("各省销售额统计报表", "UTF-8") + ".xls");
            //编码
            response.setCharacterEncoding("UTF-8");
            ExportParams ep = new ExportParams();
            ep.setStyle(ManySheetOneSyler.class);

            List<Map<String, Object>> SOTB=baseService.StatisticsOfTheBranches(mapp, hzid, ckid);
            List<ExcelExportEntity> Head = baseService.xseHeader(mapp, hzid, ckid);
            Workbook workbook = ExcelExportUtil.exportExcel(ep, Head, SOTB);
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping(value = "OrderQuantityStatistics")
    @ApiOperation(value = "各省订单量统计（按产品大类）导出报表", response = JSONObject.class, notes = "各省销售额统计导出报表")
    public @ResponseBody
    void OrderQuantityStatistics(@RequestBody Map<String, Object> mapp, HttpSession session, HttpServletResponse response) throws IOException {
        try {
            //获取当前操作用户的货主id
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid = hz.get(0).getHzid();
            //获取仓库Id
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = userinfo.getUser().getOrgId().get(0).toString();
            // 告诉浏览器用什么软件可以打开此文件
            response.setHeader("content-Type", "application/vnd.ms-excel");
            // 下载文件的默认名称
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("各省订单量统计报表", "UTF-8") + ".xls");
            //编码
            response.setCharacterEncoding("UTF-8");
            ExportParams ep = new ExportParams();
            ep.setStyle(ManySheetOneSyler.class);

            List<Map<String, Object>> ODQS=baseService.OrderQuantityStatistics(mapp, hzid, ckid);
            List<ExcelExportEntity> Head = baseService.ddlHeader(mapp, hzid, ckid);
            Workbook workbook = ExcelExportUtil.exportExcel(ep, Head, ODQS);
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
